
<!-- 
	搜索页
 -->

<template>
	<view class="search">
		<navbar :isSearch="true" v-model="value" :navcolor="navColor" @input="change"></navbar>
		<view class="search-list">
			<view v-if="is_history" class="label-box">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" :style="{color:navColor}" @click="clear">清空</text>
				</view>
				<view v-if="historyList.length > 0" class="label-content">
					<view class="label-content__item" v-for="(item, index) in historyList" :key="index" @click="openHistory(item)">
						{{ item.name }}
					</view>
				</view>
				<view v-else class="no-data">
					没有搜索历史
				</view>
			</view>
			<view v-else>
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
				<view v-if="list.length > 0">
					<videoList :list="list" @click="setHistory"></videoList>
				</view>
				<view v-else-if="list.length === 0 && !loading" class="no-data">
					没有搜索到相关视频
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	export default {
		data() {
			return {
				value: '',
				navColor: '',
				name: '',
				is_history: true,
				loading: false,
				list: []
			}
		},
		computed: {
			...mapState(['historyList'])
		},
		onLoad(option) {
			this.navColor = option.navcolor
			this.name = option.name
		},
		methods: {
			clear() {
				this.$store.dispatch('clearHistory')
				uni.showToast({
					title: "已清空"
				})
			},
			openHistory(item) {
				this.value = item.name
				this.change(this.value)
			},
			setHistory() {
				let trailer = this.historyList
				for (let i = 0; i <= trailer.length; i++) {
					if(trailer[i]) {
						let temp = trailer[i].name
						if (temp !== this.value) {
							this.$store.dispatch('set_history', {
								name: this.value
							})
							return
						} else {
							return
						}
					} else {
						this.$store.dispatch('set_history', {
							name: this.value
						})
						return
					}
				  
				  
				}
				
				
			},
			change(value) {
				if(!value) {
					clearTimeout(this.timer)
					this.mark = false
					this.getList(value)
					return
				}
				this.is_history = false
				this.loading = true
				if(!this.mark) {
					this.mark = true
					this.timer = setTimeout(()=>{
						this.mark = false
						this.getList(value)
					},1000)
				}
			},
			getList(value) {
				if(!value) {
					this.list = []
					this.is_history = true
					return
				}
				this.$api.get_list_search({
					name: this.name,
					value: value
				}).then((res)=>{
					const {data} = res
					this.loading = false
					this.list = data
				}).catch((res)=>{
					this.loading = false
				})
			}
		}
	}
</script>

<style lang="scss">
	// page {
	// 	height: 100%;
	// 	display: flex;
	// }
	.search {
		// display: flex;
		// flex-direction: column;
		// flex: 1;
		.label-box {
			margin-bottom: 10px;
			.label-header {
				display: flex;
				justify-content: space-between;
				font-size: 14px;
				color: #666;
				padding: 10px 15px;
				border-bottom: #F5F5F5 solid;
				.label-title {
					color: #8799a3;
				}
				.label-clear {
					font-weight: bold;
				}
			}
			.label-content {
				display: flex;
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;
				.label-content__item {
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border-radius: 5px;
					border: 1px #666 solid;
					font-size: 12px;
					color: #666;
				}
			}
			.no-data {
				height: 200px;
				line-height: 200px;
				width: 100%;
				text-align: center;
				font-size: 12px;
				color: #666;
			}
		}
		.no-data {
			height: 200px;
			line-height: 200px;
			width: 100%;
			text-align: center;
			font-size: 12px;
			color: #666;
		}
	}
</style>
